package edu.washington.cs.workflow.spec4j.example;

import java.util.Collection;

import org.uispec4j.Panel;
import org.uispec4j.UISpec4J;
import org.uispec4j.UISpecTestCase;
import org.uispec4j.Window;
import org.uispec4j.interception.MainClassAdapter;

import edu.washington.cs.workflow.analysis.guistructure.Spec4JUtils;
import edu.washington.cs.workflow.analysis.guistructure.uievents.MenuItemEvent;
import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent;
import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent.WINDOW;
import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEventOutcome;
import edu.washington.cs.workflow.util.Utils;

public class TestJabRefProgram extends UISpecTestCase {
	
	static {
        UISpec4J.init();
    } 
	
	protected void setUp() throws Exception {
		Class<?> clz = Class.forName("net.sf.jabref.JabRef");
	    setAdapter(new MainClassAdapter(clz, new String[0]));
	}
	
	public void testShowAllActions() {
		System.out.println("getting window");
		Window window = getMainWindow();
		System.err.println("window: " + window);
		System.err.println(window.getDescription());
        Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			System.out.println(event.toString());
		}
	}
	
	public void testImportIntoNewDb() {
		System.out.println("getting window");
		Window window = getMainWindow();
		System.err.println("window: " + window);
		System.err.println(window.getDescription());
        Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			if(event.toString().indexOf("Import into new database") != -1) {
				if(event.canFireEvent()) {
			        System.out.println(event.toString());
			        WINDOW w = event.probeWindowInfo();
			        UIEventOutcome outcome = event.fireEvent();
			        Panel panel = Spec4JUtils.getModalDialog(outcome.getTrigger());
			        System.out.println(w);
			        for(UIEvent panelEvent : Spec4JUtils.allNonNullRandomUIEvents(panel)) {
			        	System.out.println("   " + panelEvent);
			        }
			        System.out.println(panel.getDescription());
				}
			}
		}
	}
	
	public void testImportIntoCurrentDb() {
		System.out.println("getting window");
		Window window = getMainWindow();
		System.err.println("window: " + window);
		System.err.println(window.getDescription());
        Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			if(event.toString().indexOf("New database, ") != -1) {
				System.out.println(event.toString() + ",  can fire: " + event.canFireEvent());
				if(event.canFireEvent()) {
					WINDOW w = event.probeWindowInfo();
					System.out.println(w);
					event.fireEvent();
				}
			}
		}
		
		for(UIEvent event : events) {
			if(event.toString().indexOf("Import into current database") != -1) {
				System.out.println(event.toString() + ",  can fire: " + event.canFireEvent());
				if(event.canFireEvent()) {
					WINDOW w = event.probeWindowInfo();
					System.out.println(w);
					event.fireEvent();
				}
			}
		}
	}
	
	public void testExport() {
		System.out.println("getting window");
		Window window = getMainWindow();
		System.err.println("window: " + window);
		System.err.println(window.getDescription());
        Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			if(event.toString().indexOf("New database, ") != -1) {
				System.out.println(event.toString() + ",  can fire: " + event.canFireEvent());
				if(event.canFireEvent()) {
					WINDOW w = event.probeWindowInfo();
					System.out.println(w);
					event.fireEvent();
				}
			}
		}
		
		for(UIEvent event : events) {
			if(event.toString().indexOf("Export, path: MenuUI Pos: File, Export") != -1) {
				System.out.println(event.toString() + ",  can fire: " + event.canFireEvent());
				if(event.canFireEvent()) {
					WINDOW w = event.probeWindowInfo();
					System.out.println(w);
					event.fireEvent();
				}
			}
		}
	}
	
	public void testGetWindow() {
		System.out.println("getting window");
		Window window = getMainWindow();
		System.err.println("window: " + window);
		System.err.println(window.getDescription());
		
        Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		
		for(UIEvent event : events) {
			if(event.canFireEvent()) {
				if(event instanceof MenuItemEvent) {
					if(((MenuItemEvent)event).getMenuLabel().toLowerCase().trim().equals("quit")) {
						Utils.debugPrintln("Skip the quit menu");
						continue;
					}
				}
				
			    System.err.println(event);
			    event.probeWindowInfo();
			    event.fireEvent();
			}
		}
		
//		System.err.println("Number of events: " + events.size());
	}

	public static void main(String[] args) throws ClassNotFoundException {
//		net.sf.jabref.JabRef.main(new String[]{});
	}
}
